package com.CGFrame.struts.action;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.CGFrame.struts.bean.GenerateWorkbookBean;
import com.CGFrame.struts.bean.ReportDemoBean;
import com.CGFrame.struts.bean.ReportDemoBean.TaskPeriod;

public class FinishedReportAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//开始和结束日期，格式为yyyy/mm/dds
		String begin=request.getParameter("begin");
		String end=request.getParameter("end");
		String projectId=request.getParameter("projectId");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd",request.getLocale());
		Date beginDate=sdf.parse(begin);
		Date endDate=sdf.parse(end);
		Integer pId;
		if(projectId.equals("all")){
			pId=null;
		}else{
			pId=Integer.valueOf(projectId);
		}
		
		List<ReportDemoBean> beans=ReportDemoBean.getFinishedReportDemoBean(begin, end,pId);
		HSSFWorkbook wb=GenerateWorkbookBean.getWorkbook(beans,beginDate,endDate);
		
		String fileDir=request.getSession().getServletContext().getRealPath("/");
		FileOutputStream fileOut = new FileOutputStream(fileDir+"/workbook.xls");
        wb.write(fileOut);
        fileOut.flush();
        fileOut.close();
        return new ActionForward("/workbook.xls");
//		request.setAttribute("workbook", wb);
//		return mapping.findForward("showExcel");
	}
}
